<template>
	<view>
		<view v-if="flag == 0">
			<view class="list">
				<uni-list class="list-all" v-for="one in list">
					<uni-card class="card" @click="jump(one.id)" :title="one.district" extra="进入报名">
						<text class="text1">{{ one.title }}</text>
						<br />
						<br />
						<text class="text2">{{ one.createTime }}</text>
						<text class="text3" v-if="one.lea > 0" style="color: limegreen;">剩余名额:{{ one.lea }}</text>
						<text class="text3" v-if="one.lea == 0" style="color: red;">剩余名额:{{ one.lea }}</text>
					</uni-card>
				</uni-list>
			</view>
		</view>
		<view v-if="flag == 1">
			<list v-for="item in list">
				<view slot="template">
					<view class="post">
						<image :src="item.promulgatorImage" class="avatar"></image>
						<view class="info">
							<view class="name">{{ item.promulgatorName }}</view>
							<view class="time">{{ item.createTime }}</view>
							<view class="text">{{ item.content }}</view>
							<view class="image-list"><image v-for="(src, index) in item.photos" :key="index" :src="src" class="image"></image></view>
							<view class="actions">
								<view class="like" @click="handleLike(item)">
									<text class="iconfont icon-zan"></text>
									<text class="text">{{ item.likeCount > 0 ? item.likeCount : '' }}</text>
								</view>
								<view class="comment" @click="handleComment(item)">
									<text class="iconfont icon-pinglun"></text>
									<text class="text">{{ item.commentCount > 0 ? item.commentCount : '' }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</list>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			searchValue: '',
			page: 1,
			list: [],
			length: 10,
			flag: 0,
			list: []
		};
	},
	onLoad: function(options) {
		let that = this;
		that.searchValue = options.searchValue;
		that.flag = options.flag;
		if (that.flag == 0) {
			that.loadMessageList(that);
		} else if (that.flag == 1) {
			that.loadDynamicsMessage(that);
		}
	},
	onReachBottom: function() {
		let that = this;
		if (that.flag == 0) {
			if (that.isLastPage) {
				return;
			}
			that.page = that.page + 1;
			that.loadMessageList(that);
		}
	},
	methods: {
		loadDynamicsMessage(){
			let that = this;
			that.ajax(that.url.searchDynamic + "?content=" + that.searchValue, "GET", null, function(resp){
				that.list = resp.data.result;
			})
		},
		jump: function(id) {
			uni.navigateTo({
				url: '/pages/message/message?id=' + id + '&&isEnroll=0'
			});
		},
		loadMessageList: function(ref) {
			let that = this;
			let data = {
				page: ref.page,
				length: ref.length,
				key: ref.searchValue
			};
			ref.ajax(ref.url.searchAllActivityByPage, 'POST', data, function(resp) {
				let result = resp.data.result;
				console.log('result');
				console.log(result);
				if (result == null || result.length == 0) {
					ref.isLastPage = true;
					ref.page = ref.page - 1;
					uni.showToast({
						icon: 'none',
						title: '已经到底了'
					});
				} else {
					if (ref.page == 1) {
						ref.list = [];
					}
					ref.list = ref.list.concat(result);
					// if (ref.page > 1) {
					// 	uni.showToast({
					// 		icon: 'none',
					// 		title: '新增了' + result.length + '条数据'
					// 	});
					// }
				}
			});
		}
	}
};
</script>

<style lang="less">
@import url('search_result.less');
</style>
